package com.g2room.myuall.grap.univ;

import java.io.IOException;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.util.HtmlUtil;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.univ.po.UnivPo;
import com.g2room.myuall.util.DbUtil;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;

public class GrapUniv implements Runnable {
	static PreparedStatement  ps = null;
//	public static List<String> univList = new ArrayList<String>();
	static String baseUrl = "http://www.sdaxue.com";
	static List<String> listUrls = new ArrayList<String>();
	List<String> urls;
	GrapUniv(List<String> urls) {
		this.urls = urls;
	}
	GrapUniv() {}
	public static void main(String[] args) {
		getUnivInfo();
//		// 324
//		int start = 0;
//		int size = 4;
//		int end = 4;
//		System.out.println(listUrls.size());
//		for(int i = 0; i<81; i++) {
//			end = size * (i + 1);
//			GrapUniv gu = new GrapUniv(listUrls.subList(start, Math.min(end, listUrls.size())));
//			Thread t = new Thread(gu);
//			t.start();
//			start = end;
//			if(end >= listUrls.size()) break;
//		}
//		GrapUniv gu = new GrapUniv();
//		for(String url : listUrls) {
//			gu.getList(url);
//		}
//		new GrapUniv().getUnivInfo();
		String text = "{\"id\":0,\"images\":\"http://face.sdaxue.com/thumb/89fcd07f20b6785b92134bd6c1d0fa42!160x120\",\"oldSchoolId\":0,\"sourceUrl\":\"http://www.sdaxue.com/college/970.html\"}";
		UnivPo ui = JSON.parseObject(text, UnivPo.class);
		new GrapUniv().getDetail(ui);
	}
	
	public void getUnivDetail() {
		List<UnivPo> univs = getUnivs();
		for(UnivPo univ : univs) {
			updateDetail(univ);
		}
	}
	
	public static void getUnivInfo() {
//		Map<String, String> univMap = new HashMap<String, String>();
//		univMap.put("http://www.sdaxue.com/filter/___a/page/276.html", "公办");
//		univMap.put("http://www.sdaxue.com/filter/___b/page/44.html", "民办");

		for(int i =240; i< 277; i++) {
			listUrls.add("http://www.sdaxue.com/filter/___a/page/" + i + ".html");
		}
//		for(int i =126; i< 277; i++) {
//			listUrls.add("http://www.sdaxue.com/filter/___a/page/" + i + ".html");
//		}
//		for(int i =126; i< 277; i++) {
//			listUrls.add("http://www.sdaxue.com/filter/___a/page/" + i + ".html");
//		}
//		for(int i =126; i< 277; i++) {
//			listUrls.add("http://www.sdaxue.com/filter/___a/page/" + i + ".html");
//		}
//		for(int i =126; i< 277; i++) {
//			listUrls.add("http://www.sdaxue.com/filter/___a/page/" + i + ".html");
//		}
		
//		for(int i =1; i< 46; i++) {
//			listUrls.add("http://www.sdaxue.com/filter/___b/page/" + i + ".html");
//		}
//		for(int i =1; i< 2; i++) {
//			listUrls.add("http://www.sdaxue.com/filter/___c/page/" + i + ".html");
//		}
	}
	
	public void getLists() {
		for(String url : urls) {
			getList(url);
		}
	}
	/**
	 * 获取分页列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getList(String url) {
		System.out.println("start get page data!" + url);
		List<UnivPo> univs = new ArrayList<UnivPo>();
		Elements univEls = null;
		try {
			univEls = new GenericMyuall().getDoc(url).getElementsByAttributeValue("class", "results_list").get(0).getElementsByTag("li");
		} catch (Exception e) {
			System.out.println(url);
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setDescription("获取List错误："+e.getMessage());
			insert2ErrDb(ge);
		}
		for(Element univSumm : univEls) {
			UnivPo u = new UnivPo();
			Element infoEl = univSumm.getElementsByTag("a").get(0);
			u.setImages(infoEl.getElementsByTag("img").first().attr("src"));
			u.setSourceUrl(baseUrl + infoEl.attr("href"));
//			Element infoEl = univSumm.getElementsByTag("div").get(0);
//			// 获取名称
//			Element nameEl = infoEl.getElementsByTag("a").get(0);
//			u.setName(nameEl.text());
//			u.setSourceUrl(baseUrl + nameEl.attr("href"));
//			//获取简介
//			try {
//			u.setSummary(univSumm.getElementsByTag("cite").get(0).text());
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//			// 获取规模
//			u.setPersonNum(infoEl.getElementsByTag("p").get(1).text());
			univs.add(u);
		}
        
        for(UnivPo univ : univs) {
//        	try {
//				Thread.sleep(IpUtil.genRandomNum() * 500);
//			} catch (InterruptedException e) {
//				e.printStackTrace();
//			}
        	getDetail(univ);
        }
	}
	
	/**
	 * 获取列表数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void getDetail(UnivPo univ) {
		System.out.println("Name==" + univ.getName() +",start get list data!url==  " + univ.getSourceUrl());
		try {
			Document doc = new GenericMyuall().getDoc(univ.getSourceUrl());
//			// 获取建校时间和类别
//			Elements liEls = doc.getElementsByAttributeValue("class", "base_info").get(0).getElementsByTag("li");
//			// 获取规模、位置和学费
//			univ.setPosition(liEls.get(0).text());
//			String uu = liEls.get(1).text();
//			univ.setUnivUrl(null == uu || uu.trim().length() < 5 ? "" : uu.replace("官方网站：",""));
//			univ.setCategory(liEls.get(2).text());
//			univ.setCreateDate(liEls.get(3).text());
//			univ.setTuition(liEls.get(4).text());
			
			// 获取图标和英文名
			univ.setPhoto(doc.getElementsByAttributeValue("class", "logo").get(1).child(0).attr("src").replace("!48x48", ""));
//			univ.setNameEn(doc.getElementsByAttributeValue("class", "en_name").get(0).text());
//			// 获取详细内容
//			Document suDoc = new GenericMyuall().getDoc(univ.getSourceUrl().replace("college", "summary"));
//			Element summEl = suDoc.getElementsByAttributeValue("class", "summary").get(0);
//			summEl.getElementsByAttributeValue("class", "pull-right").remove();
//			univ.setDescription(summEl.html());
			insert2Db(univ);
		} catch (Exception e) {
			System.out.println(univ.getSourceUrl());
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setUrl(univ.getSourceUrl());
			ge.setType("2");
			ge.setContent(JSON.toJSONString(univ));
			ge.setDescription("获取Detail错误："+e.getMessage());
			insert2ErrDb(ge);
		}
	}
	
	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn().prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 更新数据
	 * @param url
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void updateDetail(UnivPo univ) {
		System.out.println("Name==" + univ.getName() +",start get list data!url==  " + univ.getSourceUrl());
		try {
			Document doc = new GenericMyuall().getDoc(univ.getSourceUrl());
			// 获取建校时间和类别
			Elements imgEls = doc.getElementsByAttributeValue("class", "school_photo clear").get(0).getElementsByTag("img");
			StringBuffer imgs = new StringBuffer();
			for(Element el : imgEls) {
				if(null != imgs && imgs.length() > 0) {
					imgs.append(",");
				}
				imgs.append(el.attr("src"));
			}
			univ.setImages(imgs.toString());
			
			// 获取学费、专业和招生
			Elements divEls = doc.getElementsByAttributeValue("class", "cnt");
			Element el = divEls.get(0);
			el.getElementsByTag("h6").remove();
			univ.setRecruit(el.child(0).html());
			el = divEls.get(1);
			el.getElementsByTag("h6").remove();
			univ.setTuitionDetail(el.child(0).html());
			Element majorEl = divEls.get(2);
			majorEl.getElementsByAttributeValue("class", "btn-group major_switch").remove();
			majorEl.getElementsByTag("button").remove();
			univ.setMajors(HtmlUtil.delATag(majorEl.html()).replaceAll("补充完善", "").replaceAll("none;", "block;"));
			updateDb(univ);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	int sqlCount = 0;
	public void updateDb(UnivPo univ) {
		try {
			if(sqlCount == 0) ps = DbUtil.getConn().prepareStatement("update univInfo set images=?, majors=?, recruit=?, tuitionDetail=? where id = ?");
			ps.setString(1, univ.getImages());
			ps.setString(2, univ.getMajors());
			ps.setString(3, univ.getRecruit());
			ps.setString(4, univ.getTuitionDetail());
			ps.setInt(5, univ.getId());
			ps.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void insert2Db(UnivPo univ) {
		try {
			if(sqlCount == 0) ps = DbUtil.getConn().prepareStatement("insert into univ_info1(sourceUrl,photo,images) values(?,?,?)");
			// name,sourceUrl,description,position,personNum,tuition,photo,summary,univUrl,category,createDate,nameEn
			ps.setString(1, univ.getSourceUrl());
			ps.setString(2, univ.getPhoto());
			ps.setString(3, univ.getImages());
			ps.execute();
//			if(sqlCount == 0) ps = DbUtil.getConn().prepareStatement("insert into univInfo(name,sourceUrl,description,position,personNum,tuition,photo,summary,univUrl,category,createDate,nameEn) values(?,?,?,?,?,?,?,?,?,?,?,?)");
//			// name,sourceUrl,description,position,personNum,tuition,photo,summary,univUrl,category,createDate,nameEn
//			ps.setString(1, univ.getName());
//			ps.setString(2, univ.getSourceUrl());
//			ps.setString(3, univ.getDescription());
//			ps.setString(4, univ.getPosition());
//			ps.setString(5, univ.getPersonNum());
//			ps.setString(6, univ.getTuition());
//			ps.setString(7, univ.getPhoto());
//			ps.setString(8, univ.getSummary());
//			ps.setString(9, univ.getUnivUrl());
//			ps.setString(10, univ.getCategory());
//			ps.setString(11, univ.getCreateDate());
//			ps.setString(12, univ.getNameEn());
//			ps.execute();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	public List<UnivPo> getUnivs() {
		List<UnivPo> univs = new ArrayList<UnivPo>();
		try {
			Connection connection = DbUtil.getConn();
			Statement statement = connection.createStatement();
			String sql = "select * from univinfo";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				UnivPo univ = new UnivPo();
				univ.setId(rs.getInt("id"));
				univ.setSourceUrl(rs.getString("sourceUrl"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		getLists();
	}
}
